﻿///codecombo
(function ($) {
    $.fn.codecombo = function (options, param) {
        options = options || {};
        return this.each(function () {
            var opts;
            var state = $(this).data("codecombo");
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.codecombo.defaults, options);

                var codeCategory = $(this).attr("category") ? $(this).attr("category") : opts.category;
                var codeCache = $(this).attr("cache") ? $(this).attr("cache") : opts.cache;
                if (codeCategory != undefined) {
                    var codeStatus = "0"; //$(this).attr("status") || "0";
                    //var codeSearchTemp = Constant.SysTemplate.UsrIntr.CodeTableList.replace("{0}", codeCategory).replace("{1}", codeStatus);
                    var parentGuid = "";
                    var includeSubCode = false;
                    var codeSearchTemp = SysConstant.ExtComponent.System.CodeTable.CodeTableList.Template.replace("{0}", codeCategory).replace("{1}", parentGuid).replace("{2}", includeSubCode);
                    var codeDataText;
                    if (codeCache == true) {
                        codeDataText = dataService.getCacheTransform(SysConstant.TaskGuid.ExtComponent, codeCategory, codeSearchTemp);
                    } else {
                        codeDataText = dataService.transformData(SysConstant.TaskGuid.ExtComponent, codeSearchTemp);
                    }
                    var codeDataXml = utility.xml.convertXmlStringToDoc(codeDataText);
                    var codeList = codeDataXml.find("CodeInfo");
                    var codeJson = [{ "id": "", "text": "--请选择--", "selected": true}];
                    codeList.each(function (i, n) {
                        codeJson.push({ "id": $(n).find("CodeName").text(), "text": $(n).find("CodeValue").text() });
                    });
                    if ($(this).attr("dropdownstyle") == "DropDownList") {
                        $(this).combo({
                            valueField: "id",
                            textField: "text"
                        });
                        $(this).combo("setValues", codeJson);
                    } else {
                        $(this).combobox({
                            valueField: "id",
                            textField: "text"
                        });
                        $(this).combobox("loadData", codeJson);
                    }
                }
            }
            $(this).data("codecombo", opts);
        });
    };

    $.fn.codecombo.defaults = $.extend({}, $.fn.combo.defaults, {
        cache: false
    });
})(jQuery);

///codeMulticombo
(function ($) {
    function codeMultiComboChange(target, rowData) {
        var opts = $(target).data("codeMulticombo");
        if (opts.onTextChange) {
            opts.onTextChange.call($(target), rowData.text, rowData.name);
        }
    }

    $.fn.codeMulticombo = function (options, param) {
        if (typeof options == "string") {
            return $.fn.codeMulticombo.methods[options](this, param);
        }
        options = options || {};
        return this.each(function () {
            var opts;
            var state = $(this).data("codeMulticombo");
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                var $parentCombo = $(this);
                opts = $.extend({}, $.fn.codeMulticombo.defaults, options);

                var codeCategory = $(this).attr("category");
                var codeMuti = $(this).attr("multiple");
                if (codeCategory != undefined) {

                    var codeStatus = "0"; //$(this).attr("status") || "0";
                    //var codeSearchTemp = Constant.SysTemplate.UsrIntr.CodeTableList.replace("{0}", codeCategory).replace("{1}", codeStatus);
                    var parentGuid = "";
                    var includeSubCode = false;
                    var codeJson = getChildrenNodeData(codeCategory, "", true);
                    var codeTreeID = $(this).attr("id") + "_codetree";
                    if ($("#" + codeTreeID) != undefined && $("#" + codeTreeID).length != 0) {
                        $("#" + codeTreeID).empty();
                        $("#" + codeTreeID).remove();
                    }
                    $(this).combo({});
                    $(this).combo("panel").panel({
                        width: 400,
                        content: "<table id='" + codeTreeID + "'></table>"
                    });
                    $("#" + codeTreeID).treegrid({
                        idField: "id",
                        treeField: "text",
                        height: $parentCombo.combo("options").panelHeight,
                        rownumbers: true,
                        frozenColumns: [[
                    { title: '编号', field: 'text', width: 150 }
				]],
                        fitColumns: true,
                        columns: [[{ title: '名称', field: 'name', width: 200}]],
                        singleSelect: true,
                        onClickRow: function (rowData) {
                            $parentCombo.combo("setValue", rowData.text);
                            $parentCombo.combo("setText", rowData.name);
                            codeMultiComboChange($parentCombo, rowData);
                        },
                        onDblClickRow: function (rowData) {
                            $parentCombo.combo("setValue", rowData.text);
                            $parentCombo.combo("setText", rowData.name);
                            $parentCombo.combo("hidePanel");
                            codeMultiComboChange($parentCombo, rowData);
                        }
                    });
                    $("#" + codeTreeID).treegrid("loadData", codeJson);
                }
            }
            $(this).data("codeMulticombo", opts);
        });
    };

    $.fn.codeMulticombo.parseOptions = function (target) {
        var t = $(target);
        return $.extend({}, {}, $.fn.validatebox.parseOptions(target), $.fn.combo.parseOptions(target), {
            category: (t.attr("category") || undefined)
        });
    };

    $.fn.codeMulticombo.defaults = $.extend({}, $.fn.validatebox.defaults, $.fn.combo.defaults, {
        onTextChange: function (newValue, newText) { }
    });

    $.fn.codeMulticombo.methods = {
        setValue: function () { return queryXmlTemplate; }
    };
})(jQuery);